package com.cn.esermis.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.esermis.config.RedisQueueService;
import com.cn.esermis.config.TableScheduler;
import com.cn.esermis.constant.Constant;
import com.cn.esermis.dpld.entity.*;
import com.cn.esermis.dpld.mapper.DataAcquisitionInstrumentMapper;
import com.cn.esermis.dpld.model.*;
import com.cn.esermis.dpld.service.IAnalyticalInstrumentsService;
import com.cn.esermis.dpld.service.IDataAcquisitionInstrumentService;
import com.cn.esermis.dpld.service.IDetectMonitoringFactorService;
import com.cn.esermis.dpld.service.IWaterMonitorHourRecordService;
import com.cn.esermis.dpld.service.impl.AirMonitorMinuteRecordServiceImpl;
import com.cn.esermis.excelReader.ExcelReader;
import com.cn.esermis.model.BaseUser;
import com.cn.esermis.model.ComponyData;
import com.cn.esermis.server.Consumer;
import com.cn.esermis.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.*;
import java.util.stream.Collectors;

import static com.cn.esermis.datawarn.common.DateUtil.parseDate;
import static com.cn.esermis.dpld.model.AjaxResult.error;
import static com.cn.esermis.dpld.model.AjaxResult.success;
import static com.cn.esermis.utils.TokenUtils.extractToken;

@RestController
@RequestMapping("/data_Acquisition")
public class DataAcquisitionInstrumentController {
    @Resource
    private IAnalyticalInstrumentsService analyticalInstrumentsService;
    @Resource
    private IDetectMonitoringFactorService detectMonitoringFactorService;
    @Resource
    private IDataAcquisitionInstrumentService dataAcquisitionInstrumentService;
    @Resource
    private   DataAcquisitionInstrumentMapper dataAcquisitionInstrumentMapper;
    @Resource
    private IWaterMonitorHourRecordService waterMonitorHourRecordService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private RedisQueueService redisQueueService;
    @Resource
    Consumer consumer;
    @Resource
    TableScheduler tableScheduler;
    @Autowired
    private AirMonitorMinuteRecordServiceImpl airMonitorMinuteRecordServiceImpl;

    /**
     * 获取数采仪信息表
     */
    @GetMapping("/list")
    public AjaxResult list(DataAcquisitionInstrumentVo vo, HttpServletRequest request) {
        IPage<DataAcquisitionInstrument> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        QueryWrapper<DataAcquisitionInstrument> wrapper = new QueryWrapper<>();
        BaseUser userInfo = TokenUtils.getUserInfo(extractToken(request));
        String verifyCompany = dataAcquisitionInstrumentMapper.verifyCompany(userInfo.getCompanyId());
        if (vo.getCompanyName() != null) {
            String companyId = userInfo.getCompanyId();
            if (verifyCompany != null && verifyCompany.length() < 5) {
                List<String> companyCodes = dataAcquisitionInstrumentService.getComponyListAll(vo.getCompanyName());
                if (!companyCodes.isEmpty()) {
                    wrapper.in("company_code", companyCodes);
                }else{
                    return success(new Page<>());
                }
            }else{
                List<String> companyCodes = dataAcquisitionInstrumentService.getComponyList(vo.getCompanyName(), companyId);
                if (!companyCodes.isEmpty()) {
                    wrapper.in("company_code", companyCodes);
                }
            }
        }
        if (vo.getOutletName() != null) {
            wrapper.like("outlet_name", vo.getOutletName());
        }
        wrapper.orderByDesc("department_code");
        wrapper.orderByAsc("outlet_code");
        IPage<DataAcquisitionInstrument> pageList = dataAcquisitionInstrumentService.page(page, wrapper);
        IPage<DataAcquisitionInstrumentVo> volits = new Page<>();
        if (!pageList.getRecords().isEmpty()) {
            volits.setTotal(pageList.getTotal());
            volits.setCurrent(pageList.getCurrent());
            volits.setSize(pageList.getSize());
            List<DataAcquisitionInstrument> records = pageList.getRecords();
            List<DataAcquisitionInstrumentVo> revoList = new ArrayList<>();
            // 查询企业
            Map<String, Object> componyMap = dataAcquisitionInstrumentService.getComponyMap();
            // 查询工厂
            Map<String, Object> departmentMap = dataAcquisitionInstrumentService.getDepartmentMap();
            records.forEach(x -> {
                DataAcquisitionInstrumentVo instrumentVo = new DataAcquisitionInstrumentVo();
                List<AnalyticalInstruments> analyticalInstruments = analyticalInstrumentsService.getByDataAcquisitionInstrumentid(x.getId());
                BeanUtils.copyProperties(x, instrumentVo);
                if (instrumentVo.getCompanyCode() != null && componyMap.get(instrumentVo.getCompanyCode()) != null) {
                    Map componyMapobj = (Map) componyMap.get(instrumentVo.getCompanyCode());
                    instrumentVo.setCompanyName(componyMapobj.get("companylongname").toString());
                }
                if (instrumentVo.getDepartmentCode() != null && departmentMap.get(instrumentVo.getDepartmentCode()) != null) {
                    Map componyMapobj = (Map) departmentMap.get(instrumentVo.getDepartmentCode());
                    instrumentVo.setDepartmentName(componyMapobj.get("departmentname").toString());
                }
                ;
                if (!analyticalInstruments.isEmpty()) {
                    List<AnalyticalInstrumentsVo> analyticalInstrumentList = new ArrayList<>();
                    analyticalInstruments.forEach(y -> {
                        AnalyticalInstrumentsVo analyticalInstrumentsVo = new AnalyticalInstrumentsVo();
                        BeanUtils.copyProperties(y, analyticalInstrumentsVo);
                        analyticalInstrumentList.add(analyticalInstrumentsVo);
                        List<DetectMonitoringFactor> analyticalInstrumentId = detectMonitoringFactorService.listByParentId(y.getId());
                        if (!analyticalInstrumentId.isEmpty()) {
                            analyticalInstrumentId.forEach(z -> {
                                analyticalInstrumentsVo.getDetectMonitoringFactors().add(z);
                            });
                        }
                    });
                    instrumentVo.setAnalyticalInstruments(analyticalInstrumentList);
                }
                revoList.add(instrumentVo);
            });
            volits.setRecords(revoList);
        }
        List<OutletAndFactorVo> selectlist = dataAcquisitionInstrumentService.selectlist();
        redisTemplate.opsForValue().set("dataAcquisitionInstruments", JSON.toJSONString(selectlist));
        return success(volits);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping(value = "delete/{id}")
    public AjaxResult delete(@PathVariable String id) {
        DataAcquisitionInstrument dataAcquisitionInstrument = dataAcquisitionInstrumentService.getById(id);
        DataAcquisitionInstrumentVo instrumentVo = new DataAcquisitionInstrumentVo();
        if (dataAcquisitionInstrument != null) {
            BeanUtils.copyProperties(dataAcquisitionInstrument, instrumentVo);
            List<AnalyticalInstruments> analyticalInstruments = analyticalInstrumentsService.getByDataAcquisitionInstrumentid(dataAcquisitionInstrument.getId());
            if (!analyticalInstruments.isEmpty()) {
                List<AnalyticalInstrumentsVo> analyticalInstrumentList = new ArrayList<>();
                analyticalInstruments.forEach(y -> {
                    if (y.getId() != null)
                        detectMonitoringFactorService.remove(new QueryWrapper<DetectMonitoringFactor>().eq("analytical_instrument_id", y.getId()));
                });
                if (analyticalInstruments.isEmpty())
                    analyticalInstrumentsService.remove(new QueryWrapper<AnalyticalInstruments>().eq("data_acquisition_instrument_id", dataAcquisitionInstrument.getId()));
                instrumentVo.setAnalyticalInstruments(analyticalInstrumentList);
            }
        }
        dataAcquisitionInstrumentService.remove(new QueryWrapper<DataAcquisitionInstrument>().eq("id", id));
        List<OutletAndFactorVo> selectlist = dataAcquisitionInstrumentService.selectlist();
        redisTemplate.opsForValue().set("dataAcquisitionInstruments", JSON.toJSONString(selectlist));
        return success();
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        DataAcquisitionInstrument dataAcquisitionInstrument = dataAcquisitionInstrumentService.getById(id);
        DataAcquisitionInstrumentVo instrumentVo = new DataAcquisitionInstrumentVo();
        if (dataAcquisitionInstrument != null) {
            BeanUtils.copyProperties(dataAcquisitionInstrument, instrumentVo);
            List<AnalyticalInstruments> analyticalInstruments = analyticalInstrumentsService.getByDataAcquisitionInstrumentid(dataAcquisitionInstrument.getId());
            if (!analyticalInstruments.isEmpty()) {
                List<AnalyticalInstrumentsVo> analyticalInstrumentList = new ArrayList<>();
                analyticalInstruments.forEach(x -> {
                    AnalyticalInstrumentsVo analyticalInstrumentsVo = new AnalyticalInstrumentsVo();
                    BeanUtils.copyProperties(x, analyticalInstrumentsVo);

                    List<DetectMonitoringFactor> analyticalInstrumentId = detectMonitoringFactorService.listByParentId(x.getId());
                    if (!analyticalInstrumentId.isEmpty()) {
                        analyticalInstrumentId.forEach(z -> {
                            analyticalInstrumentsVo.getDetectMonitoringFactors().add(z);
                        });
                    }
                    analyticalInstrumentList.add(analyticalInstrumentsVo);
                });
                instrumentVo.setAnalyticalInstruments(analyticalInstrumentList);
            }
        }
        List<OutletAndFactorVo> selectlist = dataAcquisitionInstrumentService.selectlist();
        redisTemplate.opsForValue().set("dataAcquisitionInstruments", JSON.toJSONString(selectlist));
        return success(instrumentVo);
    }


    /**
     * 新增数采仪信息
     */
    @PostMapping("/save")
    @Transactional
    public AjaxResult save(@Validated @RequestBody DataAcquisitionInstrumentVo vo) {
        DataAcquisitionInstrument dataAcquisitionInstrument = new DataAcquisitionInstrument();
        BeanUtils.copyProperties(vo, dataAcquisitionInstrument);
        // 修改企业信息
        if (vo.getCompanyInfo() != null) {
            List<DataAcquisitionInstrument> list = dataAcquisitionInstrumentService.list(new QueryWrapper<DataAcquisitionInstrument>()
                    .eq("industry_category", vo.getCompanyInfo().getIndustryCategory())
                    .eq("license_number", vo.getCompanyInfo().getLicenseNumber())
                    .eq("license_data", vo.getCompanyInfo().getLicenseData())
                    .eq("company_type", vo.getCompanyInfo().getCompanyType())
            );
            if (!list.isEmpty()){
                list.forEach(x->{
                    x.setIndustryCategory(vo.getIndustryCategory());
                    x.setLicenseNumber(vo.getLicenseNumber());
                    x.setLicenseData(vo.getLicenseData());
                    x.setCompanyType(vo.getCompanyType());
                    dataAcquisitionInstrumentService.updateById(x);
                });
            }
        }
        boolean save = dataAcquisitionInstrumentService.saveOrUpdate(dataAcquisitionInstrument);
        if (save && vo.getAnalyticalInstruments() != null) {
            List<AnalyticalInstrumentsVo> analyticalInstrumentList = vo.getAnalyticalInstruments();
            // 删除子项 全删全增操作
            {
                if (dataAcquisitionInstrument.getId() != null) {
                    List<AnalyticalInstruments> dataAcquisitionInstrumentId = analyticalInstrumentsService.getByDataAcquisitionInstrumentid(dataAcquisitionInstrument.getId());
                    if (!dataAcquisitionInstrumentId.isEmpty()) {
                        analyticalInstrumentList.forEach(y -> {
                            detectMonitoringFactorService.remove(new QueryWrapper<DetectMonitoringFactor>().eq("analytical_instrument_id", y.getId()));
                        });
                        analyticalInstrumentsService.remove(new QueryWrapper<AnalyticalInstruments>().eq("data_acquisition_instrument_id", dataAcquisitionInstrument.getId()));
                    }
                }
            }
            // 新增子表
            analyticalInstrumentList.forEach(x -> {
                AnalyticalInstruments analyticalInstruments = new AnalyticalInstruments();
                BeanUtils.copyProperties(x, analyticalInstruments);
                analyticalInstruments.setDataAcquisitionInstrumentId(dataAcquisitionInstrument.getId());
                boolean save1 = analyticalInstrumentsService.save(analyticalInstruments);
                if (x.getDetectMonitoringFactors() != null && save1) {
                    List<DetectMonitoringFactor> detectMonitoringFactors = x.getDetectMonitoringFactors();
                    detectMonitoringFactors.forEach(y -> {
                        y.setAnalyticalInstrumentId(analyticalInstruments.getId());
                        y.setDataAcquisitionInstrumentId(dataAcquisitionInstrument.getId());
                        boolean save2 = detectMonitoringFactorService.save(y);
                        if (!save2) {
                            throw new RuntimeException("添加失败");
                        }
                    });
                }
            });
        }
        List<OutletAndFactorVo> selectlist = dataAcquisitionInstrumentService.selectlist();
        redisTemplate.opsForValue().set("dataAcquisitionInstruments", JSON.toJSONString(selectlist));
        return success();
    }

    @GetMapping(value = "getCompony")
    public AjaxResult getCompony() {
        List<ComponyData> list = dataAcquisitionInstrumentService.getCompony();
        return success(list);
    }



    /**
     * 部门
     *
     * @return
     */
    @GetMapping(value = "getDepartment/{componyid}")
    public AjaxResult getDepartment(@PathVariable String componyid) {
        List<Map<String, Object>> list = dataAcquisitionInstrumentService.getDepartment(componyid);
        return success(list);
    }

    /**
     * 部门
     *
     * @return
     */
    @GetMapping(value = "getUser")
    public AjaxResult getUser(HttpServletRequest request) {
        BaseUser userInfo = TokenUtils.getUserInfo(extractToken(request));
        return success(userInfo);
    }


    /**
     * 获取数采仪信息表
     */
    @GetMapping("/listByCode")
    public AjaxResult listByCode(HttpServletRequest request) {
        QueryWrapper<DataAcquisitionInstrument> wrapper = new QueryWrapper<>();
        BaseUser userInfo = TokenUtils.getUserInfo(extractToken(request));
        if (userInfo == null) {
            return error("无数据");
        }
        wrapper.eq(userInfo.getCompanyId() != null, "company_code", userInfo.getCompanyId());
        wrapper.eq(userInfo.getFactoryId() != null, "department_code", userInfo.getFactoryId());
//        List<DataAcquisitionInstrument> dataAcquisitionInstruments = dataAcquisitionInstrumentService.listByCode(vo);
        List<DataAcquisitionInstrument> dataAcquisitionInstruments = dataAcquisitionInstrumentService.list(wrapper);
        return success(dataAcquisitionInstruments);
    }



    /**
     * 获取数采仪信息表
     */
    @GetMapping("/totalNumberOfLinks")
    public AjaxResult totalNumberOfLinks(HttpServletRequest request) {
        return success(redisQueueService.getNumber(Constant.SERVER_ONLINE));
    }

    @GetMapping("/getCompanyInfo/{companyCode}/{departmentCode}")
    public AjaxResult getCompanyInfo(@PathVariable String companyCode,@PathVariable String departmentCode) {
        return success(dataAcquisitionInstrumentMapper.getCompanyInfo(companyCode,departmentCode));
    }





    @GetMapping("/getDataList")
    public AjaxResult getDataList() {
        QueryWrapper<WaterMonitorHourRecord> q = new QueryWrapper<>();
        return success(waterMonitorHourRecordService.list(q));
    }


    /**
     * 获取数采仪信息表
     */
    @GetMapping("/daoru")
    public AjaxResult daoru(HttpServletRequest request) {

        ExcelReader excelReader = new ExcelReader();
        List<DataAllVo> data = excelReader.getData();
//        System.out.println(data);
        List<Department> departmentList =  dataAcquisitionInstrumentService.getDepartmentDit();
        // 添加公司编号
        for (DataAllVo datum : data) {
            Optional<Department> first = departmentList.stream().filter(d -> d.getDepartmentname().equals(datum.getCompanyCode())).findFirst();
            if (first.isPresent()){
                datum.setCompanyCode(first.get().getCompanyid());
                datum.setDepartmentCode(first.get().getDepartmentid());
            }
        }
        Map<String, List<DataAllVo>> collect = data.stream().collect(Collectors.groupingBy(d -> d.getOutletName() + "_" + d.getLicenseNumber()));
        for (Map.Entry<String, List<DataAllVo>> entry : collect.entrySet()) {
            List<DataAllVo> value = entry.getValue();
            DataAcquisitionInstrument dataAcquisitionInstrument = new DataAcquisitionInstrument();
            value.forEach(x->{
                if (x.getOutletName()!=null && !x.getOutletName().isEmpty()){
                    dataAcquisitionInstrument.setOutletName(x.getOutletName());
                }
                if (x.getOutletCode()!=null && !x.getOutletCode().isEmpty()){
                    dataAcquisitionInstrument.setOutletCode(x.getOutletCode());
                }
                if (x.getType()!=null){
                    dataAcquisitionInstrument.setType(x.getType());
                }
                if (x.getUseData()!=null){
                    dataAcquisitionInstrument.setUseData(x.getUseData());
                }
                if (x.getCompleteFilings()!=null){
                    dataAcquisitionInstrument.setCompleteFilings(x.getCompleteFilings());
                }
                if (x.getCompleteFilingsTime()!=null){
                    dataAcquisitionInstrument.setCompleteFilingsTime(x.getCompleteFilingsTime());
                }
                if (x.getCollectionName()!=null && !x.getCollectionName().isEmpty()){
                    dataAcquisitionInstrument.setCollectionName(x.getCollectionName());
                }
                if (x.getCollectionBrand()!=null && !x.getCollectionBrand().isEmpty()){
                    dataAcquisitionInstrument.setCollectionBrand(x.getCollectionBrand());
                }
                if (x.getCollectionModel()!=null && !x.getCollectionModel().isEmpty()){
                    dataAcquisitionInstrument.setCollectionModel(x.getCollectionModel());
                }
                if (x.getMnCode()!=null && !x.getMnCode().isEmpty()){
                    dataAcquisitionInstrument.setMnCode(x.getMnCode());
                }
                if (x.getCollectionIp()!=null && !x.getCollectionIp().isEmpty()){
                    dataAcquisitionInstrument.setCollectionIp(x.getCollectionIp());
                }
                if (x.getCompanyCode()!=null && !x.getCompanyCode().isEmpty()){
                    dataAcquisitionInstrument.setCompanyCode(x.getCompanyCode());
                }
                if (x.getDepartmentCode()!=null && !x.getDepartmentCode().isEmpty()){
                    dataAcquisitionInstrument.setDepartmentCode(x.getDepartmentCode());
                }
                if (x.getHaveExhaust()!=null){
                    dataAcquisitionInstrument.setHaveExhaust(x.getHaveExhaust());
                }
                if (x.getExhaustTemperature()!=null && !x.getExhaustTemperature().isEmpty()){
                    dataAcquisitionInstrument.setExhaustTemperature(x.getExhaustTemperature());
                }
                if (x.getExhaustHeight()!=null && !x.getExhaustHeight().isEmpty()){
                    dataAcquisitionInstrument.setExhaustHeight(x.getExhaustHeight());
                }
                if (x.getExhaustDiameter()!=null && !x.getExhaustDiameter().isEmpty()){
                    dataAcquisitionInstrument.setExhaustDiameter(x.getExhaustDiameter());
                }
                if (x.getIndustryCategory()!=null && !x.getIndustryCategory().isEmpty()){
                    dataAcquisitionInstrument.setIndustryCategory(x.getIndustryCategory());
                }
                if (x.getLicenseNumber()!=null && !x.getLicenseNumber().isEmpty()){
                    dataAcquisitionInstrument.setLicenseNumber(x.getLicenseNumber());
                }
                if (x.getLicenseData()!=null && !x.getLicenseData().isEmpty()){
                    dataAcquisitionInstrument.setLicenseData(x.getLicenseData());
                }
            });
            dataAcquisitionInstrumentService.save(dataAcquisitionInstrument);
            // 解析分析仪数据
            Map<String, List<DataAllVo>> brandCollect = value.stream().collect(Collectors.groupingBy(DataAllVo::getBrand));
            for (Map.Entry<String, List<DataAllVo>> listEntry : brandCollect.entrySet()) {
                AnalyticalInstruments analyticalInstruments = new AnalyticalInstruments();
                List<DataAllVo> dataAllVos = listEntry.getValue();
                analyticalInstruments.setDataAcquisitionInstrumentId(dataAcquisitionInstrument.getId());
                dataAllVos.forEach(x->{
                    if (x.getBrand()!=null && !x.getBrand().isEmpty()){
                        analyticalInstruments.setBrand(x.getBrand());
                    }
                    if (x.getModel()!=null && !x.getModel().isEmpty()){
                        analyticalInstruments.setModel(x.getModel());
                    }
                    if (x.getName()!=null && !x.getName().isEmpty()){
                        analyticalInstruments.setName(x.getName());
                    }
                    if (x.getFactorInfor()!=null && !x.getFactorInfor().isEmpty()){
                        analyticalInstruments.setFactorInfor(x.getFactorInfor());
                    }
//                    if (x.getRange()!=null && !x.getRange().isEmpty()){
//                        analyticalInstruments.setRange(x.getRange());
//                    }
                    if (x.getRange()!=null && !x.getRange().isEmpty())   {
                        analyticalInstruments.setRange(x.getRange());
                    }
                    if(x.getReferenceOxygenContent()!=null && !x.getReferenceOxygenContent().isEmpty()){
                        analyticalInstruments.setReferenceOxygenContent(x.getReferenceOxygenContent());
                    }
                    if(x.getVelocityFieldCoefficient()!=null && !x.getVelocityFieldCoefficient().isEmpty()){
                        analyticalInstruments.setVelocityFieldCoefficient(x.getVelocityFieldCoefficient());
                    }
                    if(x.getPitotTubeCoefficient()!=null && !x.getPitotTubeCoefficient().isEmpty()){
                        analyticalInstruments.setPitotTubeCoefficient(x.getPitotTubeCoefficient());
                    }
                    if(x.getKvalue()!=null && !x.getKvalue().isEmpty()){
                        analyticalInstruments.setKvalue(x.getKvalue());
                    }
                    if(x.getFlueArea()!=null && !x.getFlueArea().isEmpty()){
                        analyticalInstruments.setFlueArea(x.getFlueArea());
                    }
                    if(x.getDilutionRatio()!=null && !x.getDilutionRatio().isEmpty()){
                        analyticalInstruments.setDilutionRatio(x.getDilutionRatio());
                    }
                    if(x.getFxytype()!=null){
                        analyticalInstruments.setType(x.getFxytype());
                    }
                });
                analyticalInstrumentsService.save(analyticalInstruments);
                dataAllVos.forEach(x->{
                    DetectMonitoringFactor detectMonitoringFactor = new DetectMonitoringFactor();
                    detectMonitoringFactor.setAnalyticalInstrumentId(analyticalInstruments.getId());
                    detectMonitoringFactor.setDataAcquisitionInstrumentId(dataAcquisitionInstrument.getId());
                    if (x.getFactor()!=null && !x.getFactor().isEmpty()){
                        detectMonitoringFactor.setFactor(x.getFactor());
                    }
                    if (x.getHoursAvg()!=null && !x.getHoursAvg().isEmpty()){
                        detectMonitoringFactor.setHoursAvg(x.getHoursAvg());
                    }
                    if (x.getHoursUnit()!=null && !x.getHoursUnit().isEmpty()){
                        detectMonitoringFactor.setHoursUnit(x.getHoursUnit());
                    }
                    if (x.getMonthlyEmissions()!=null && !x.getMonthlyEmissions().isEmpty()){
                        detectMonitoringFactor.setMonthlyEmissions(x.getMonthlyEmissions());
                    }
                    if (x.getMonthlyEmissionsUnit()!=null && !x.getMonthlyEmissionsUnit().isEmpty()){
                        detectMonitoringFactor.setMonthlyEmissionsUnit(x.getMonthlyEmissionsUnit());
                    }
                    if (x.getYearEmissions()!=null && !x.getYearEmissions().isEmpty()){
                        detectMonitoringFactor.setYearEmissions(x.getYearEmissions());
                    }
                    if (x.getYearEmissionsUnit()!=null && !x.getYearEmissionsUnit().isEmpty()){
                        detectMonitoringFactor.setYearEmissionsUnit(x.getYearEmissionsUnit());
                    }
                    detectMonitoringFactor.setRange(x.getRangeStr());
                    detectMonitoringFactor.setRangeUnit(x.getRangeUnit());
                    detectMonitoringFactor.setFactorName(x.getFactorName());
                    detectMonitoringFactorService.save(detectMonitoringFactor);
                });
            }
        }
        return success();
    }


    @GetMapping("/test")
    public AjaxResult test(Integer pageNum, Integer size) {
        QueryWrapper<AirMonitorMinuteRecord> wrapper = new QueryWrapper<>();
        IPage<AirMonitorMinuteRecord> page = new Page<>(pageNum, size);
        wrapper.between("creat_time", parseDate("2025-02-01"),parseDate("2025-03-31"));
        IPage<AirMonitorMinuteRecord> paged = airMonitorMinuteRecordServiceImpl.page(page,wrapper);
        return success(paged);
    }

    @GetMapping("/tableScheduler")
    public AjaxResult tableScheduler() {

        tableScheduler.preCreateNextMonthTable();
        return success("");
    }

}
